<template>
  <el-container direction="vertical">

    <navMenu width="100%"></navMenu>

    <el-container direction="horizontal">
      <el-aside width="280px" height="100%">
        <sideBar class="menu" page-index="1-4" open-index="1"></sideBar>
      </el-aside>
      <el-main>
        <!--        <p>this is staff page</p>-->
        <!--搜索栏-->
        <el-container direction="horizontal">
          <el-input v-model="words.word" placeholder="请输入员工名字或员工编号（支持模糊查询）"></el-input>
          <el-button style="margin-left: 20px" @click="vague()">搜索</el-button>
          <el-button @click="drawer = true" type="primary" style="margin-left: 16px;">添加</el-button>
          <el-drawer
            title="新增员工"
            :visible.sync="drawer"
            :direction="direction"
            ref="thisdrawer">
<!--            :before-close="handleClose"-->

            <el-row :gutter="20" style="margin-bottom: 12px">
              <el-col :span="6" :offset="2" >员工编号</el-col>

            </el-row>
            <el-row :gutter="20" style="margin-bottom: 30px">
              <el-col :span="20" :offset="2"><el-input v-model="newsubinfo.staff_id" placeholder="请输入新员工编号" clearable ></el-input></el-col>
            </el-row>
            <el-row :gutter="20" style="margin-bottom: 12px">
              <el-col :span="6" :offset="2">员工姓名</el-col>
            </el-row>
            <el-row :gutter="20" style="margin-bottom: 30px">
              <el-col :span="20" :offset="2"><el-input v-model="newsubinfo.staff_name" placeholder="请输入新员工姓名" clearable ></el-input></el-col>
            </el-row>
            <el-row :gutter="20" style="margin-bottom: 12px">
              <el-col :span="6" :offset="2">联系方式</el-col>
            </el-row>
            <el-row :gutter="20" style="margin-bottom: 45px">
              <el-col :span="20" :offset="2"><el-input v-model="newsubinfo.staff_phone" placeholder="请输入新员工联系方式" clearable ></el-input></el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="10" :offset="6">
                <el-button type="primary" style="width: 130%" @click="newstaff()">确认提交</el-button></el-col>
            </el-row>
          </el-drawer>
        </el-container>
        <!--员工列表-->
        <el-table
          :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
          @selection-change="handleSelectionChange"
          height="502"
          style="width: 100%; margin-top: 20px">

          <el-table-column
            prop="staff_id"
            label="员工编号"
            width="200px">
          </el-table-column>

          <el-table-column
            prop="staff_name"
            label="员工姓名" >
          </el-table-column>

          <el-table-column
            prop="staff_phone"
            label="联系方式">
          </el-table-column>

          <el-table-column label="操作" width="150px">
            <template slot-scope="scope">

              <el-button icon="el-icon-edit" circle size="small"
                         @click="drawer2 = true,editsubinfo.staff_id=scope.row.staff_id,
                         editsubinfo.staff_name=scope.row.staff_name,
                         editsubinfo.staff_phone=scope.row.staff_phone"></el-button>

              <el-drawer
                title="修改员工"
                :visible.sync="drawer2"
                :direction="direction"
                ref="thisdrawer2">

                <el-row :gutter="20" style="margin-bottom: 12px">
                  <el-col :span="6" :offset="2">员工编号</el-col>
                </el-row>
                <el-row :gutter="20" style="margin-bottom: 30px">
                  <el-col :span="6" :offset="2"><div v-text="editsubinfo.staff_id"/></el-col>
                </el-row>
                <el-row :gutter="20" style="margin-bottom: 12px">
                  <el-col :span="6" :offset="2">员工姓名</el-col>
                </el-row>
                <el-row :gutter="20" style="margin-bottom: 30px">
                  <el-col :span="20" :offset="2"><el-input v-model="editsubinfo.staff_name" placeholder="请输入新员工姓名" clearable ></el-input></el-col>
                </el-row>
                <el-row :gutter="20" style="margin-bottom: 12px">
                  <el-col :span="6" :offset="2">联系方式</el-col>
                </el-row>
                <el-row :gutter="20" style="margin-bottom: 40px">
                  <el-col :span="20" :offset="2"><el-input v-model="editsubinfo.staff_phone" placeholder="请输入新员工联系方式" clearable ></el-input></el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="6" :offset="6">
                    <el-button type="primary" style="width: 130%" @click="handleEdit()">确认修改</el-button></el-col>
                </el-row>
              </el-drawer>
              <el-button icon="el-icon-delete" circle size="small"
                         @click="handleDelete(scope.$index, scope.row)"></el-button>
            </template>
          </el-table-column>

        </el-table>
        <!--角标-->
        <div style="text-align: center;margin-top: 20px;">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            @current-change="current_change">
          </el-pagination>
        </div>

      </el-main>
    </el-container>
  </el-container>

</template>

<script>
import sideBar from "@/components/sideBar";
import navMenu from "@/components/navMenu";
export default {
  components: {sideBar, navMenu},
  data() {
    return {
      tableData: [],
      total: 0,
      pagesize: 8,
      currentPage: 1,
      st: 2,
      words: {
        word: ''
      },
      drawer: false,
      drawer2: false,
      direction: 'rtl',

      newsubinfo: {
        staff_id: '', staff_name: '',staff_phone: ''
      },

      editsubinfo: {
        staff_id: '', staff_name: '',staff_phone: ''
      }
    }
  },
  mounted() {
    this.$axios.get(`http://127.0.0.1:5000/staff/init`).then((res) => {
      console.log(res);
      this.tableData = res.data;
      this.total = this.tableData.length+3;
      console.log(res.data);
    });
  },
  methods: {
    current_change: function (currentPage) {
      this.currentPage = currentPage;
    }, async vague() {
      this.$axios.post(`http://127.0.0.1:5000/staff/select`, this.words).then((res) => {
        console.log(res);
        if(res.data==0){
          this.$message('没有符合查询条件的结果');
        }
        else {
          this.tableData = res.data;
          this.total = this.tableData.length+3;
        }
      });
    }, async handleDelete($index, row) {
      console.log(row.staff_id);
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.$axios.post(`http://127.0.0.1:5000/staff/delete`, row.staff_id).then((res) => {
          console.log(res.data);
          this.st = res.data;
          if (this.st==1){
            this.$message('删除成功');
            window.location.reload();}
          else{
            this.$message('删除失败');
          }

          this.st = 2;
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    async newstaff() {
      if (this.newsubinfo.staff_id=='')
        this.$message('请输入员工编号');
      else if(this.newsubinfo.staff_name=='')
        this.$message('请输入员工姓名');
      else
        this.$axios.post(`http://127.0.0.1:5000/staff/newstaff`, this.newsubinfo).then((res) => {
          console.log(res);
          this.st = res.data;

          if (this.st==1) {
            this.$message('新增成功');
            this.$refs.thisdrawer.closeDrawer();
            window.location.reload();
          }
          else {
            this.$message('新增失败，工号已存在！');
            this.$refs.thisdrawer.closeDrawer();
          }
          this.st=2;
        });
    },
    handleEdit() {
      if (this.editsubinfo.staff_id=='')
        this.$message('请输入员工编号');
      else if(this.editsubinfo.staff_name=='')
        this.$message('请输入员工姓名');
      else
        this.$axios.post(`http://127.0.0.1:5000/staff/editstaff`, this.editsubinfo).then((res) => {
          console.log(res);
          this.st = res.data;

          if (this.st==1) {
            this.$message('修改成功');
            this.$refs.thisdrawer2.closeDrawer();
            window.location.reload();
          }
          else{
            this.$message('修改失败！');
            this.$refs.thisdrawer2.closeDrawer();
          }
          this.st.statu=2;
        });
    }
  }
}
</script>

<style>

.menu{
  width: 100%;
  float: left;
  height: 700px;
}


</style>

